/* Name: RandomEventGenerator
 * Author: Cameron Hoerig
 * Date: 5-22-12
 * Description: The RandomEventGenerator class is invoked every 30 seconds and creates a supernova 
 *  or blackhole with/without a GammaRayBurst dependent on their respective probabilities
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

public class RandomEventGenerator extends Item{
	
	// get probabilities for associated events
	private float blackholeProb;
	private float gammaProb;
	private float supernovaProb;
	
	// Only add RandomEventGenerator to the queue upon creation as this object will delete
	// itself after running
	public RandomEventGenerator(){
		Game.create((Item)this, 1); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking
		
		switch (Game.difficulty) {
			case Game.DIFFICULTY_EASY:
				blackholeProb = 0.3f;
				gammaProb = 0.0f;
				supernovaProb = 0.2f;
				break;
			case Game.DIFFICULTY_MEDIUM:
				blackholeProb = 0.5f;
				gammaProb = 0.2f;
				supernovaProb = 0.3f;
				break;
			case Game.DIFFICULTY_HARD:
				blackholeProb = 0.7f;
				gammaProb = 0.4f;
				supernovaProb = 0.4f;
				break;
		}
	}
	
	// create random events
	public void update() {
		final int numSupernova = Supernova.num;
		float newProb = Game.randomFloat(1f);
		if(newProb <= supernovaProb & numSupernova < 1){ // if supernova is to be created, ensure no other supernova currently exists. If one does exists,
			Planet pid = Planet.getRemovableConqueredPlanet();
			if (pid != null) {
				float dir = Game.randomFloat((float) (2.0f*Math.PI));
				int dis = pid.r + 20;
				int tmpX = pid.x + (int) (dis*Math.cos(dir));
				int tmpY = pid.y + (int) (dis*Math.sin(dir));
				new Supernova(tmpX, tmpY); // a new one is not created
			}
		}
		
		newProb = Game.randomFloat(1f);
		Boolean addGamma = false;
		if(newProb <= blackholeProb){ 
			Wormhole wid = Wormhole.getRandomWormhole();
			if (wid != null) {
				int tmpX = (wid.x + wid.x2)/2;
				int tmpY = (wid.y + wid.y2)/2;
				newProb = Game.randomFloat(1f);// if blackhole is to be created, determine is it is to have a GammaRayBurst
				if(newProb <= gammaProb){
					addGamma = true;
				}
				new Blackhole(tmpX, tmpY, addGamma);
			}
		}
		// finished with random event generation. Delete this object.
		Game.delete(this);
	}
}
